/**
 * Created by lenovo on 2017/2/21.
 */
//主页面
'use strict'
const express=require('express');
const path=require('path');
const bodyParser = require('body-parser');
//存储用户信息
const session = require('express-session')
//创建app

const app=express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 300000 }}))
app.use(express.static(path.join(__dirname,'./src/statics/')));
//权限验证
app.all('/*',(req,res,next)=>{
    if(req.url != '/account/login' && req.url != '/account/register'){
        if(req.session.username!=null){
            //有登录
            //console.log(req.session.username);
            next();
        }else{
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end("<script>alert('请登录');window.location.href='/account/login/'</script>")
        }
    }else {
        next();
    }
})
//登录页面
const accountRouter = require(path.join(__dirname,'./src/routers/loginRouter.js'));
app.use('/account',accountRouter);
//管理页面
const infoRouter= require(path.join(__dirname,'./src/routers/infoRoute.js'))
app.use('/student',infoRouter);
//开启服务器
app.listen(5217,()=>{
    console.log('open');
})